import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateWorksTagDto } from './dto/create-works-tag.dto';
import { UpdateWorksTagDto } from './dto/update-works-tag.dto';
import { WorksTagEntity } from './entities/works-tag.entity';

@Injectable()
export class WorksTagService {
  constructor(
    @InjectRepository(WorksTagEntity)
    private readonly workTagRepository: Repository<WorksTagEntity>,
  ) {}

  async create(name) {
    return await this.workTagRepository.save({ name });
  }

  async findByIds(ids: string[]) {
    return this.workTagRepository.findByIds(ids);
  }

  async findAll(query) {
    const { pageNum = 1, pageSize = 100, ...params } = query;
    const result = await this.workTagRepository.findAndCount({
      order: {
        id: 'DESC',
      },
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
    });
    const list = result[0].map((item) => item);
    return { list, count: result[1] };
  }

  remove(id: number) {
    return `This action removes a #${id} worksTag`;
  }
}
